﻿using System.Windows.Data;
using System.Windows.Media;
using System.Collections;
using System;
namespace DeepEarth.Toolkit.Common.Converters
{
    public class DoubleCollectionConverter : IValueConverter
    {
        public static DoubleCollection Convert(IEnumerable value)
        {
            DoubleCollection retVal = new DoubleCollection();

            if (value != null)
            {
                foreach (var v in value)
                {
                    double d;
                    if (double.TryParse(v.ToString(), out d))
                    {
                        retVal.Add(d);
                    }
                }
            }

            return retVal;
        }

        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType == typeof(DoubleCollection))
            {
                return Convert(value as IEnumerable);
            }
            else
            {
                throw new ArgumentException(string.Format("Cannot convert {0} to DoubleCollection", value.GetType().Name));
            }
        }

        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new System.NotImplementedException();
        }
    }
}
